﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks;

namespace RemotingLibs
{
    public class ClientBase
    {
        private RemotingBase _remoting;
        private readonly object _lockHelper = new object();
        private readonly string _ip;
        private readonly int _port;
        private readonly string _clientMark;

        public ClientBase(string ip, int port, string clientMark)
        {
            _ip = ip;
            _port = port;
            _clientMark = clientMark;
        }

        #region Tcp信道

        /// <summary>
        /// 利用Tcp信道，可以用于不同的计算机之间的程序通讯，速度比Http信道快，比Ipc信道慢
        /// </summary>
        /// <returns></returns>
        public RemotingBase GetTcpInstance()
        {
            if (_remoting == null)
            {
                lock (_lockHelper)
                {
                    if (_remoting == null)
                    {
                        TcpClientChannel clientChannel = new TcpClientChannel("Tcp" + _clientMark + _port,
                            new BinaryClientFormatterSinkProvider());
                        ChannelServices.RegisterChannel(clientChannel, false);

                        _remoting = (RemotingBase)Activator.GetObject(typeof(RemotingBase),
                            string.Format("tcp://{0}:{1}/TcpRemoting{1}", _ip, _port), WellKnownObjectMode.Singleton);

                    }
                }
            }

            return _remoting;
        }

        #endregion

        #region Http信道

        /// <summary>
        /// 利用Http信道，可以用于不同的计算机之间的程序通讯，速度比Tcp信道慢
        /// </summary>
        /// <returns></returns>
        public RemotingBase GetHttpInstance()
        {
            if (_remoting == null)
            {
                lock (_lockHelper)
                {
                    if (_remoting == null)
                    {
                        HttpClientChannel clientChannel = new HttpClientChannel("Http" + _clientMark + _port,
                            new BinaryClientFormatterSinkProvider());
                        ChannelServices.RegisterChannel(clientChannel, false);


                        _remoting = (RemotingBase)Activator.GetObject(typeof(RemotingBase),
                            string.Format("http://{0}:{1}/HttpRemoting{1}", _ip, _port), WellKnownObjectMode.Singleton);
                    }
                }
            }

            return _remoting;
        }

        #endregion

        #region Ipc信道

        /// <summary>
        /// 利用Ipc信道，只能用于同一台计算机的程序之间通讯，通讯速度最快
        /// </summary>
        /// <returns></returns>
        public RemotingBase GetIpcInstance()
        {
            if (_remoting == null)
            {
                lock (_lockHelper)
                {
                    if (_remoting == null)
                    {
                        IpcClientChannel clientChannel = new IpcClientChannel("Ipc" + _clientMark,
                            new BinaryClientFormatterSinkProvider());
                        ChannelServices.RegisterChannel(clientChannel, false);

                        _remoting = (RemotingBase)Activator.GetObject(typeof(RemotingBase),
                            string.Format("ipc://{0}/{1}", "Ipc", "IpcRemoting"), WellKnownObjectMode.Singleton);

                    }
                }
            }

            return _remoting;
        }

        #endregion
    }
}
